package net.realsee.service.wcs.device.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.device.WcsVehicleEqpt;
import net.realsee.domain.wcs.device.bo.*;
import net.realsee.domain.wcs.device.vo.*;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.device.WcsVehicleEqptMapper;
import net.realsee.service.wcs.device.IWcsEquipmentService;
import net.realsee.service.wcs.device.IWcsVehicleEqptService;
import net.realsee.service.wcs.device.IWcsVehicleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆设备关系Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsVehicleEqptServiceImpl extends ServiceImpl<WcsVehicleEqptMapper, WcsVehicleEqpt> implements IWcsVehicleEqptService {

    private final WcsVehicleEqptMapper baseMapper;

    private final IWcsEquipmentService iWcsEquipmentService;

    private final IWcsVehicleService iWcsVehicleService;

    /**
     * 查询车辆设备关系
     */
    @Override
    public WcsVehicleEqptVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车辆设备关系列表
     */
    @Override
    public TableDataInfo<WcsVehicleEqptVo> selectVoPage(WcsVehicleEqptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsVehicleEqpt> lqw = buildVoQueryWrapper(bo);
        Page<WcsVehicleEqptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车辆设备关系列表
     */
    @Override
    public List<WcsVehicleEqptVo> selectVoList(WcsVehicleEqptBo bo) {
        LambdaQueryWrapper<WcsVehicleEqpt> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsVehicleEqpt> buildVoQueryWrapper(WcsVehicleEqptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsVehicleEqpt> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsVehicleEqpt::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleNo()), WcsVehicleEqpt::getVehicleNo, bo.getVehicleNo());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipment()), WcsVehicleEqpt::getEquipment, bo.getEquipment());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsVehicleEqpt::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsVehicleEqpt::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsVehicleEqpt::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsVehicleEqpt::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsVehicleEqpt::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsVehicleEqpt::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsVehicleEqpt::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsVehicleEqpt::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsVehicleEqpt::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsVehicleEqpt::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsVehicleEqpt::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsVehicleEqpt::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsVehicleEqpt::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增车辆设备关系
     */
    @Override
    public Boolean insertByBo(WcsVehicleEqptBo bo) {
        WcsVehicleEqpt add = BeanUtil.toBean(bo, WcsVehicleEqpt.class);
        return this.save(add);
    }

    /**
     * 批量新增
     *
     * @param list
     * @return
     */
    @Override
    public Boolean insertByBatch(List<WcsVehicleEqpt> list) {
        this.baseMapper.insertBatch(list);
        return true;
    }

    /**
     * 修改车辆设备关系
     */
    @Override
    public Boolean updateByBo(WcsVehicleEqptBo bo) {
        WcsVehicleEqpt update = BeanUtil.toBean(bo, WcsVehicleEqpt.class);
        return this.updateById(update);
    }

    /**
     * 批量删除车辆设备关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 获取设备信息
     *
     * @param id
     * @return
     */
    @Override
    public EquipmentBindVehicleVo getEquipmentInfo(Long id) {
        EquipmentBindVehicleVo equipmentBindVehicleVo = new EquipmentBindVehicleVo();
        WcsEquipmentVo vo = this.iWcsEquipmentService.selectVoById(id);
        if (vo != null) {
            BeanUtil.copyProperties(vo, equipmentBindVehicleVo);
            List<WcsVehicleVo> vehicleVos = this.selectVehicleByequipment(vo.getEquipment());
            if (CollectionUtil.isNotEmpty(vehicleVos)) {
                equipmentBindVehicleVo.setBindingVehicles(vehicleVos);
            }
        }
        return equipmentBindVehicleVo;
    }


    /**
     * 查询设备已绑定的车辆
     *
     * @param equipment
     * @return
     */
    public List<WcsVehicleVo> selectVehicleByequipment(String equipment) {
        WcsVehicleEqptBo bo = new WcsVehicleEqptBo();
        bo.setEquipment(equipment);
        List<WcsVehicleEqptVo> vehicleEqptVos = this.selectVoList(bo);
        List<WcsVehicleVo> vehicleVos = this.iWcsVehicleService.selectVoList(new WcsVehicleBo());
        for (WcsVehicleVo vehicleVo : vehicleVos) {
            for (WcsVehicleEqptVo vehicleEqptVo : vehicleEqptVos) {
                if (vehicleVo.getVehicleNo().equals(vehicleEqptVo.getVehicleNo())) {
                    vehicleVo.setFlag(true);
                    break;
                }
            }
        }
        return vehicleVos;
    }

    /**
     * 设备绑定车辆
     *
     * @param bo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertEqptVehicle(BindingEquipmentVehicleBo bo) {
        WcsVehicleEqptBo qbo = new WcsVehicleEqptBo();
        qbo.setEquipment(bo.getEquipment());
        List<WcsVehicleEqptVo> voList = this.selectVoList(qbo);
        if (CollectionUtil.isNotEmpty(voList)) {
            //先清空
            List<Long> ids = voList.stream().map(WcsVehicleEqptVo::getId).collect(Collectors.toList());
            this.deleteWithValidByIds(ids);
        }
        if (ArrayUtil.isNotEmpty(bo.getVehicleNos())) {
            // 新增设备与车辆管理
            List<WcsVehicleEqpt> list = new ArrayList<>(bo.getVehicleNos().length);
            for (String vehicleNo : bo.getVehicleNos()) {
                WcsVehicleEqpt vehicleEquipment = new WcsVehicleEqpt();
                vehicleEquipment.setEquipment(bo.getEquipment());
                vehicleEquipment.setVehicleNo(vehicleNo);
                list.add(vehicleEquipment);
            }
            this.insertByBatch(list);
        }
    }

    /**
     * 获取车辆信息
     *
     * @param id
     * @return
     */
    @Override
    public VehicleBindEquipmentVo getVehicleInfo(Long id) {
        WcsVehicleVo vo = this.iWcsVehicleService.selectVoById(id);
        VehicleBindEquipmentVo vehicleBindEquipmentVo = new VehicleBindEquipmentVo();
        if (vo != null) {
            BeanUtil.copyProperties(vo, vehicleBindEquipmentVo);
            List<WcsEquipmentVo> equipmentVos = this.selectEqptByVehicleNo(vo.getVehicleNo());
            if (CollectionUtil.isNotEmpty(equipmentVos)) {
                vehicleBindEquipmentVo.setBindingEquipments(equipmentVos);
            }
        }
        return vehicleBindEquipmentVo;
    }

//    @Override
//    public void insertVehicleEqpt(BindingVehicleEqptBo bo) {
//
//    }


    /**
     * 查询车辆已绑定设备
     *
     * @param vehicleNo
     * @return
     */
    private List<WcsEquipmentVo> selectEqptByVehicleNo(String vehicleNo) {
        WcsVehicleEqptBo bo = new WcsVehicleEqptBo();
        bo.setVehicleNo(vehicleNo);
        List<WcsVehicleEqptVo> vehicleEqptVos = this.selectVoList(bo);
        List<WcsEquipmentVo> EquipmentVos = this.iWcsEquipmentService.selectVoList(new WcsEquipmentBo());
        for (WcsEquipmentVo equipmentVo : EquipmentVos) {
            for (WcsVehicleEqptVo vehicleEqptVo : vehicleEqptVos) {
                if (equipmentVo.getEquipment().equals(vehicleEqptVo.getEquipment())) {
                    equipmentVo.setFlag(true);
                    break;
                }
            }
        }
        return EquipmentVos;
    }

    /**
     * 添加车辆绑定设备关系
     *
     * @param bo
     * @return
     */
    @Override
    public void insertVehicleEqpt(BindingVehicleEquipmentBo bo) {
        WcsVehicleEqptBo qbo = new WcsVehicleEqptBo();
        qbo.setVehicleNo(bo.getVehicleNo());
        List<WcsVehicleEqptVo> voList = this.selectVoList(qbo);
        if (CollectionUtil.isNotEmpty(voList)) {
            //先清空
            List<Long> ids = voList.stream().map(WcsVehicleEqptVo::getId).collect(Collectors.toList());
            this.deleteWithValidByIds(ids);
        }
        if (ArrayUtil.isNotEmpty(bo.getEquipments())) {
            // 新增用户与角色管理
            List<WcsVehicleEqpt> list = new ArrayList<>(bo.getEquipments().length);
            for (String equiment : bo.getEquipments()) {
                WcsVehicleEqpt vehicleEquipment = new WcsVehicleEqpt();
                vehicleEquipment.setEquipment(equiment);
                vehicleEquipment.setVehicleNo(bo.getVehicleNo());
                list.add(vehicleEquipment);
            }
            this.insertByBatch(list);
        }
    }
}
